package stack_em_up;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

class Main implements Runnable
{
	  static String ReadLn(int maxLength) 
	    {
	        byte line[] = new byte[maxLength];
	        int length = 0;
	        int input = -1;
	        try {
	            while (length < maxLength) {
	                input = System.in.read();
	                if ((input < 0) || (input == '\n'))
	                    break;
	                line[length++] += input;
	            }

	            if ((input < 0) && (length == 0))
	                return null;
	            return new String(line, 0, length);
	        } catch (IOException e) {
	            return null;
	        }
	    }
	  
	public static void main(String [] args)
	{
		Main stuff = new Main();
		stuff.run();
	}
	
	public void run()
	{
		new StackEmUp().run();
	}
}

class StackEmUp implements Runnable
{
	private static final int MAX_LENGTH = 1024;
	
	public void run()
	{
		int testCaseCount;
		int numOfShuffles;
		
		String line = Main.ReadLn(MAX_LENGTH);
		line = line.trim();
		testCaseCount = Integer.valueOf(line);
		
		Main.ReadLn(MAX_LENGTH);
		
		for (int i = 0; i < testCaseCount; i++)
		{
			if (i != 0) 
            {
                System.out.println();
            }
			
			numOfShuffles = Integer.parseInt(Main.ReadLn(MAX_LENGTH).trim());
			
			//
			ArrayList<String []> shuffleList = new ArrayList<String []>();
			
			// No element in zero index.
			shuffleList.add(null);
			
			int shuffleCount = 0;
			int counter = 1;
			String [] deckArray;
			String [] deckArray2;
			
			String [] temp;
			
			while (shuffleCount < numOfShuffles) 
			{
				deckArray = Main.ReadLn(MAX_LENGTH).trim().split(" ");

				if (deckArray.length < 52) 
				{
					deckArray2 = Main.ReadLn(MAX_LENGTH).trim().split(" ");

					deckArray = concat(deckArray, deckArray2);
				}

				temp = new String[53];

				for (int j = 0; j < deckArray.length; j++) 
				{
					temp[j + 1] = deckArray[j];
				}

				shuffleList.add(counter, temp);

				shuffleCount++;
				counter++;
			}
			//

			//
			// Read k between 1 and n.
			String k;
			ArrayList<String> cardObs = new ArrayList<String>();

			while ((k = Main.ReadLn(MAX_LENGTH)) != null) 
			{
				k = k.trim();
				
				if (k.isEmpty())
				{
					break;
				}
				
				cardObs.add(k);
			}
			//
			
			Card [] deck = createDeck();
			
			deck = stackEmUp(shuffleList, cardObs, deck);
			
			printResults(deck);
		}
		while (Main.ReadLn(MAX_LENGTH) != null);
	}

	private void printResults(Card[] deck) 
	{
		for (int i = 1; i < deck.length; i++)
		{
			System.out.println(deck[i].value + " of " + deck[i].suit);
		}
	}
	
	public Card [] stackEmUp(ArrayList<String []> shuffleList, ArrayList<String> cardObs, Card [] deck)
	{
		
		for (int i = 0; i < cardObs.size(); i++)
		{
			// Retrieve the shuffle type.
			int shuffleType = Integer.parseInt(cardObs.get(i));
			
			String [] shuffle = shuffleList.get(shuffleType);
			
			deck = performShuffle(shuffle, deck);
		}
		
		return deck;
	}
	
	public Card []  performShuffle(String [] shuffle, Card [] deck)
	{
			int position;
			
			Card [] tempDeck = new Card[53];
			
			for (int index = 1; index < shuffle.length; index++)
			{
				// Retrieve card location.
				int cardLocation = Integer.parseInt(shuffle[index]);
				
				// Retrieve card.
				Card selectedCard = deck[cardLocation];
				
				tempDeck[index] = selectedCard;
			}
			
			return tempDeck;
	}
	
	public ArrayList<String []> loadShuffles(int numOfShuffles)
	{
		ArrayList<String []> shuffleList = new ArrayList<String []>();
		
		// No element in zero index.
		shuffleList.add(null);
		
		int shuffleCount = 0;
		int counter = 1;
		String [] deckArray;
		String [] deckArray2;
		
		String [] temp;
		
		while (shuffleCount < numOfShuffles) 
		{
			deckArray = Main.ReadLn(MAX_LENGTH).trim().split(" ");

			if (deckArray.length < 52) 
			{
				deckArray2 = Main.ReadLn(MAX_LENGTH).split(" ");

				deckArray = concat(deckArray, deckArray2);
			}

			temp = new String[53];

			for (int i = 0; i < deckArray.length; i++) 
			{
				temp[i + 1] = deckArray[i];
			}

			shuffleList.add(counter, temp);

			shuffleCount++;
			counter++;
		}
		 
		return shuffleList;
	}
	
	public ArrayList<String> loadShuffleObs() 
	{
		// Read k between 1 and n.
		String k = "";
		ArrayList<String> cardObs = new ArrayList<String>();

		while (!(k = Main.ReadLn(MAX_LENGTH)).equals("")) 
		{
			cardObs.add(k);
		}
		
		return cardObs;
	}
	
	public <T> T[] concat(T[] first, T[] second) 
	{
		  T[] result = Arrays.copyOf(first, first.length + second.length);
		  System.arraycopy(second, 0, result, first.length, second.length);
		  return result;
	}
	
	public Card [] createDeck()
	{
		String [] suits = {"Clubs" , "Diamonds", "Hearts", "Spades"};
		Card [] deck = new Card[53];
		
		int position = 1;
		String value = "";
		
		for (int i = 0; i < suits.length; i++)
		{
			String selectedSuit = suits[i];
			
			for (int j = 2; j <= 14; j++)
			{
				switch(j)
				{
				case 11:
					value = "Jack";
					break;
				case 12:
					value = "Queen";
					break;
				case 13:
					value = "King";
					break;
				case 14:
					value = "Ace";
					break;
				default:
					value = "" + j;
					break;
				}
				
				// Create new card.
				Card card = new Card(value, selectedSuit);
				
				deck[position] = card;
				
				position++;
				// System.out.println(deck.indexOf(card) + ": " + "{ " + card.getValue() + ", " + card.getSuit() + " }");
			}
		}
		return deck;
	}
}

class Card
{
	String value;
	int position;
	String suit;
	public Card(String value, String suit)
	{
		this.value = value;
		this.suit = suit;
	}
	
	public String getValue()
	{
		return value;
	}
	
	public String getSuit()
	{
		return suit;
	}
}